package wqas

import (
	"bufio"
	"encoding/json"
	"fmt"
	"io"
	"log"
	"math/rand"
	"net/http"
	"os"
	"strconv"
	"time"

	"github.com/sirupsen/logrus"
)

type ImprovementHandler struct {
}

func NewImprovementHandler() *ImprovementHandler {
	// 设置logrus的Formatter为textformatter，并开启显示函数名
	return &ImprovementHandler{}
}

func (ctx *ImprovementHandler) Sleep(shouldSleep bool) int {
	if !shouldSleep {
		return 0
	}
	r := rand.Intn(3) + 2
	time.Sleep(time.Duration(r) * time.Second)
	return r
}

/* 获取机器人的连接状态 */
func (ctx *ImprovementHandler) GetStatistics(w http.ResponseWriter, r *http.Request) {
	logrus.Debug("Entry")
	logrus.Print("GetCobotStatus")
	if r.Method == "GET" || r.Method == "POST" {
		// w.WriteHeader(http.StatusNotFound)
		// w.Write([]byte("oops"))
		// return
		// 读取文件内容
		inputFile := "webserver/assets/wqas/improvement/getStatistics_en.json"

		inputReader, error := os.Open(inputFile)
		if error != nil {
			logrus.Error(error)
		} else {
			// buf, err := os.ReadFile(inputFile)
			inputReader := bufio.NewReader(inputReader)
			// for {
			// 	inputBytes, err := inputReader.ReadBytes('\n') // 循环读取文件内容，每次以 '\n'字符为一次读取的终止
			// 	inputString := string(inputBytes)
			// 	fmt.Printf("== inputString: %v\n", inputString)
			// 	if err == io.EOF { // 文件结束
			// 		return
			// 	}
			// }
			// inputReader = bufio.NewReader(inputReader)
			w.Header().Set("Content-Type", "application/json")
			r.ParseForm()
			// io.WriteString()
			io.Copy(w, inputReader)
		}
	}
}

/*
每日报告
*/
func (ctx *ImprovementHandler) GetDailyReport(w http.ResponseWriter, r *http.Request) {
	logrus.Info("GetDailyReport ")
	if r.Method == "GET" {
		r.ParseForm()
		page, _ := strconv.Atoi(r.Form.Get("page"))
		pageSize, _ := strconv.Atoi(r.Form.Get("pageSize"))

		reviewDate := r.Form.Get("reviewDate")

		logrus.Info("page: ", page, " pageSize: ", pageSize, " reviewDate: ", reviewDate)

		inputFile := "webserver/assets/wqas/improvement/getDailyReport_en.json"

		inputReader, error := os.Open(inputFile)
		if error != nil {
			logrus.Error(error)
		} else {
			inputReader := bufio.NewReader(inputReader)
			w.Header().Set("Content-Type", "application/json")
			r.ParseForm()
			io.Copy(w, inputReader)
		}
	}
}

/*
批量报告
*/
func (ctx *ImprovementHandler) GetBatchReport(w http.ResponseWriter, r *http.Request) {
	t := ctx.Sleep(false)
	switch r.Method {
	case "GET":
		w.Header().Set("Content-Type", "application/json")
		// 写入 sessionid 的方式代替 token
		r.ParseForm()
		page, _ := strconv.Atoi(r.Form.Get("page"))
		pageSize, _ := strconv.Atoi(r.Form.Get("pageSize"))

		reportDate := r.Form.Get("reportDate")
		topDefect := r.Form.Get("topDefect")

		logrus.Info("page: ", page, " pageSize: ", pageSize, " reportDate: ", reportDate, " topDefect: ", topDefect)

		inputFile := "webserver/assets/wqas/improvement/getBatchReport_en.json"

		inputReader, error := os.Open(inputFile)
		if error != nil {
			logrus.Error(error)
		} else {
			inputReader := bufio.NewReader(inputReader)
			w.Header().Set("Content-Type", "application/json")
			r.ParseForm()
			io.Copy(w, inputReader)
		}
	case "POST":
		ip := r.FormValue("ip")

		var response = make(map[string]interface{})
		if len(ip) > 0 {
			response["code"] = 200
			response["msg"] = "机械臂未连接"
			response["data"] = map[string]string{"msg": "connect success", "rand": strconv.Itoa(t), "status": "0"}
		} else {
			response["code"] = -1
			response["msg"] = "failure"
			response["data"] = map[string]string{"msg": "The IP cannot be empty", "rand": strconv.Itoa(t), "status": "-1"}
		}

		jsonByte, _ := json.Marshal(response)
		str := string(jsonByte)
		log.Println("res: ", str)
		fmt.Println("res: ", str)
		io.WriteString(w, str)
	}
}

/*
打开电源
*/
func (ctx *ImprovementHandler) GetImprovementRecord(w http.ResponseWriter, r *http.Request) {
	t := ctx.Sleep(false)
	switch r.Method {
	case "GET":
		w.Header().Set("Content-Type", "application/json")
		// 写入 sessionid 的方式代替 token
		r.ParseForm()
		page, _ := strconv.Atoi(r.Form.Get("page"))
		pageSize, _ := strconv.Atoi(r.Form.Get("pageSize"))

		reportDate := r.Form.Get("reportDate")
		topDefect := r.Form.Get("topDefect")

		logrus.Info("page: ", page, " pageSize: ", pageSize, " reportDate: ", reportDate, " topDefect: ", topDefect)

		inputFile := "webserver/assets/wqas/improvement/getImprovementRecord_en.json"

		inputReader, error := os.Open(inputFile)
		if error != nil {
			logrus.Error(error)
		} else {
			inputReader := bufio.NewReader(inputReader)
			w.Header().Set("Content-Type", "application/json")
			r.ParseForm()
			io.Copy(w, inputReader)
		}
	case "POST":
		ip := r.FormValue("ip")

		var response = make(map[string]interface{})
		if len(ip) > 0 {
			response["code"] = 200
			response["msg"] = "机械臂未连接"
			response["data"] = map[string]string{"msg": "connect success", "rand": strconv.Itoa(t), "status": "0"}
		} else {
			response["code"] = -1
			response["msg"] = "failure"
			response["data"] = map[string]string{"msg": "The IP cannot be empty", "rand": strconv.Itoa(t), "status": "-1"}
		}

		jsonByte, _ := json.Marshal(response)
		str := string(jsonByte)
		log.Println("res: ", str)
		fmt.Println("res: ", str)
		io.WriteString(w, str)
	}
}

/*
批量报告
*/
func (ctx *ImprovementHandler) GetDailyStatistics(w http.ResponseWriter, r *http.Request) {
	t := ctx.Sleep(false)
	switch r.Method {
	case "GET":
		w.Header().Set("Content-Type", "application/json")
		// 写入 sessionid 的方式代替 token
		r.ParseForm()
		page, _ := strconv.Atoi(r.Form.Get("page"))
		pageSize, _ := strconv.Atoi(r.Form.Get("pageSize"))

		// defectType := r.Form.Get("defectType")
		date := r.Form.Get("date")

		logrus.Info("page: ", page, " pageSize: ", pageSize, " date: ", date)

		inputFile := "webserver/assets/wqas/improvement/getDailyStatistics.json"

		inputReader, error := os.Open(inputFile)
		if error != nil {
			logrus.Error(error)
		} else {
			inputReader := bufio.NewReader(inputReader)
			w.Header().Set("Content-Type", "application/json")
			r.ParseForm()
			io.Copy(w, inputReader)
		}
	case "POST":
		ip := r.FormValue("ip")

		var response = make(map[string]interface{})
		if len(ip) > 0 {
			response["code"] = 200
			response["msg"] = "机械臂未连接"
			response["data"] = map[string]string{"msg": "connect success", "rand": strconv.Itoa(t), "status": "0"}
		} else {
			response["code"] = -1
			response["msg"] = "failure"
			response["data"] = map[string]string{"msg": "The IP cannot be empty", "rand": strconv.Itoa(t), "status": "-1"}
		}

		jsonByte, _ := json.Marshal(response)
		str := string(jsonByte)
		log.Println("res: ", str)
		fmt.Println("res: ", str)
		io.WriteString(w, str)
	}
}

/*
批量报告
*/
func (ctx *ImprovementHandler) GetDailyDetailByDefect(w http.ResponseWriter, r *http.Request) {
	t := ctx.Sleep(true)
	switch r.Method {
	case "GET":
		w.Header().Set("Content-Type", "application/json")
		// 写入 sessionid 的方式代替 token
		r.ParseForm()
		page, _ := strconv.Atoi(r.Form.Get("page"))
		pageSize, _ := strconv.Atoi(r.Form.Get("pageSize"))

		reportDate := r.Form.Get("reportDate")
		topDefect := r.Form.Get("topDefect")

		logrus.Info("page: ", page, " pageSize: ", pageSize, " reportDate: ", reportDate, " topDefect: ", topDefect)

		inputFile := "webserver/assets/wqas/improvement/getDailyStatisticsByDefectType.json"

		inputReader, error := os.Open(inputFile)
		if error != nil {
			logrus.Error(error)
		} else {
			inputReader := bufio.NewReader(inputReader)
			w.Header().Set("Content-Type", "application/json")
			r.ParseForm()
			io.Copy(w, inputReader)
		}
	case "POST":
		ip := r.FormValue("ip")

		var response = make(map[string]interface{})
		if len(ip) > 0 {
			response["code"] = 200
			response["msg"] = "机械臂未连接"
			response["data"] = map[string]string{"msg": "connect success", "rand": strconv.Itoa(t), "status": "0"}
		} else {
			response["code"] = -1
			response["msg"] = "failure"
			response["data"] = map[string]string{"msg": "The IP cannot be empty", "rand": strconv.Itoa(t), "status": "-1"}
		}

		jsonByte, _ := json.Marshal(response)
		str := string(jsonByte)
		log.Println("res: ", str)
		fmt.Println("res: ", str)
		io.WriteString(w, str)
	}
}

func (ctx *ImprovementHandler) GenerateDailyReport(w http.ResponseWriter, r *http.Request) {
	t := ctx.Sleep(false)
	switch r.Method {
	case "GET":
		w.Header().Set("Content-Type", "application/json")
		// 写入 sessionid 的方式代替 token
		r.ParseForm()
		page, _ := strconv.Atoi(r.Form.Get("page"))
		pageSize, _ := strconv.Atoi(r.Form.Get("pageSize"))

		reportDate := r.Form.Get("reportDate")
		topDefect := r.Form.Get("topDefect")

		logrus.Info("page: ", page, " pageSize: ", pageSize, " reportDate: ", reportDate, " topDefect: ", topDefect)

		inputFile := "webserver/assets/wqas/improvement/createDailyReport_en.json"

		inputReader, error := os.Open(inputFile)
		if error != nil {
			logrus.Error(error)
		} else {
			inputReader := bufio.NewReader(inputReader)
			w.Header().Set("Content-Type", "application/json")
			r.ParseForm()
			io.Copy(w, inputReader)
		}
	case "POST":
		ip := r.FormValue("ip")

		var response = make(map[string]interface{})
		if len(ip) > 0 {
			response["code"] = 200
			response["msg"] = "机械臂未连接"
			response["data"] = map[string]string{"msg": "connect success", "rand": strconv.Itoa(t), "status": "0"}
		} else {
			response["code"] = -1
			response["msg"] = "failure"
			response["data"] = map[string]string{"msg": "The IP cannot be empty", "rand": strconv.Itoa(t), "status": "-1"}
		}

		jsonByte, _ := json.Marshal(response)
		str := string(jsonByte)
		log.Println("res: ", str)
		fmt.Println("res: ", str)
		io.WriteString(w, str)
	}
}

func (ctx *ImprovementHandler) GenerateBatchReport(w http.ResponseWriter, r *http.Request) {
	t := ctx.Sleep(false)
	switch r.Method {
	case "GET":
		w.Header().Set("Content-Type", "application/json")
		// 写入 sessionid 的方式代替 token
		r.ParseForm()
		page, _ := strconv.Atoi(r.Form.Get("page"))
		pageSize, _ := strconv.Atoi(r.Form.Get("pageSize"))

		reportDate := r.Form.Get("reportDate")
		topDefect := r.Form.Get("topDefect")

		logrus.Info("page: ", page, " pageSize: ", pageSize, " reportDate: ", reportDate, " topDefect: ", topDefect)

		inputFile := "webserver/assets/wqas/improvement/createBatchReport_en.json"

		inputReader, error := os.Open(inputFile)
		if error != nil {
			logrus.Error(error)
		} else {
			inputReader := bufio.NewReader(inputReader)
			w.Header().Set("Content-Type", "application/json")
			r.ParseForm()
			io.Copy(w, inputReader)
		}
	case "POST":
		ip := r.FormValue("ip")

		var response = make(map[string]interface{})
		if len(ip) > 0 {
			response["code"] = 200
			response["msg"] = "机械臂未连接"
			response["data"] = map[string]string{"msg": "connect success", "rand": strconv.Itoa(t), "status": "0"}
		} else {
			response["code"] = -1
			response["msg"] = "failure"
			response["data"] = map[string]string{"msg": "The IP cannot be empty", "rand": strconv.Itoa(t), "status": "-1"}
		}

		jsonByte, _ := json.Marshal(response)
		str := string(jsonByte)
		log.Println("res: ", str)
		fmt.Println("res: ", str)
		io.WriteString(w, str)
	}
}

func (ctx *ImprovementHandler) PushDailyReport(w http.ResponseWriter, r *http.Request) {
	t := ctx.Sleep(false)
	switch r.Method {
	case "GET":
		w.Header().Set("Content-Type", "application/json")
		// 写入 sessionid 的方式代替 token
		r.ParseForm()
		page, _ := strconv.Atoi(r.Form.Get("page"))
		pageSize, _ := strconv.Atoi(r.Form.Get("pageSize"))

		reportDate := r.Form.Get("reportDate")
		topDefect := r.Form.Get("topDefect")

		logrus.Info("page: ", page, " pageSize: ", pageSize, " reportDate: ", reportDate, " topDefect: ", topDefect)

		inputFile := "webserver/assets/wqas/improvement/pushDailyReport_en.json"

		inputReader, error := os.Open(inputFile)
		if error != nil {
			logrus.Error(error)
		} else {
			inputReader := bufio.NewReader(inputReader)
			w.Header().Set("Content-Type", "application/json")
			r.ParseForm()
			io.Copy(w, inputReader)
		}
	case "POST":
		ip := r.FormValue("ip")

		var response = make(map[string]interface{})
		if len(ip) > 0 {
			response["code"] = 200
			response["msg"] = "机械臂未连接"
			response["data"] = map[string]string{"msg": "connect success", "rand": strconv.Itoa(t), "status": "0"}
		} else {
			response["code"] = -1
			response["msg"] = "failure"
			response["data"] = map[string]string{"msg": "The IP cannot be empty", "rand": strconv.Itoa(t), "status": "-1"}
		}

		jsonByte, _ := json.Marshal(response)
		str := string(jsonByte)
		log.Println("res: ", str)
		fmt.Println("res: ", str)
		io.WriteString(w, str)
	}
}

func (ctx *ImprovementHandler) PushBatchReport(w http.ResponseWriter, r *http.Request) {
	t := ctx.Sleep(false)
	switch r.Method {
	case "GET":
		w.Header().Set("Content-Type", "application/json")
		// 写入 sessionid 的方式代替 token
		r.ParseForm()
		page, _ := strconv.Atoi(r.Form.Get("page"))
		pageSize, _ := strconv.Atoi(r.Form.Get("pageSize"))

		reportDate := r.Form.Get("reportDate")
		topDefect := r.Form.Get("topDefect")

		logrus.Info("page: ", page, " pageSize: ", pageSize, " reportDate: ", reportDate, " topDefect: ", topDefect)

		inputFile := "webserver/assets/wqas/improvement/pushBatchReport_en.json"

		inputReader, error := os.Open(inputFile)
		if error != nil {
			logrus.Error(error)
		} else {
			inputReader := bufio.NewReader(inputReader)
			w.Header().Set("Content-Type", "application/json")
			r.ParseForm()
			io.Copy(w, inputReader)
		}
	case "POST":
		ip := r.FormValue("ip")

		var response = make(map[string]interface{})
		if len(ip) > 0 {
			response["code"] = 200
			response["msg"] = "机械臂未连接"
			response["data"] = map[string]string{"msg": "connect success", "rand": strconv.Itoa(t), "status": "0"}
		} else {
			response["code"] = -1
			response["msg"] = "failure"
			response["data"] = map[string]string{"msg": "The IP cannot be empty", "rand": strconv.Itoa(t), "status": "-1"}
		}

		jsonByte, _ := json.Marshal(response)
		str := string(jsonByte)
		log.Println("res: ", str)
		fmt.Println("res: ", str)
		io.WriteString(w, str)
	}
}
